<?php
namespace Wap\Model;
use Think\Model;

class ShowModel extends Model{

	protected $tableName = "praise_item";
	protected $patchValidate = true;
	protected $_validate = array(
		array( "uid", "require", "用户不存在" ),//上传者ID;
		array( "teacher_telephone", "/(^(13|14|15|18|17)\d{9}$)/", "手机号码格式不正确", 0, "regex" ),
		array( "teacher_name", "require", "教师姓名不能为空", 1, "regex" ),
		array( "parent_telephone", "/(^(13|14|15|18|17)\d{9}$)/", "手机号码格式不正确", 0, "regex" ),
		array( "parent_name", "require", "家长姓名不能为空", 1, "regex" ),
		array( "pic", "require", "没有作品", 1, "" ),
		array( "show_name", "require", "没有作品名称", 1, "" ),
		array( "author_name", "require", "作者名字不能为空", 1, "" ),
		array( "grade", array( 1, 2, 3, 4, 5, 6 ), "作者名字不能为空", 1, "in" ),
		array( "birth_time", "require", "出生日期不能为空", 1, "" ),
		array( "birth_file", "require", "出生证明不能为空", 1, "" ),
		array( "school_name", "require", "学校名字不能空", 1, "" ),
		array( "address", "require", "地址不能空", 1, "" ),
	);

	public function fetch_all()
	{
		$result = $this->order( "votes desc" )->select();
		return $result;
	}

	public function fetch_all_by_id( $id )
	{
		$data = $this->where( "id=%d", array( $id ) )->find();
		return $data;
	}

	public function fetch_rank_by_votes( $votes )
	{
		$rank =	$this->distinct( true )->field( "votes" )->where( "votes >= %d", array( $votes ) )->count();
		$last = $this->distinct( true )->field( "votes" )->where( "votes > %d", array( $votes ) )->order( "votes asc" )->limit( 1 )->find();
		
		$result = array( "index" => $rank, "gap" => 0, "percent" => '100%' );
		if( count( $last ) > 0 ){
			$result['gap'] = $last['votes'] - $votes;
			$result['percent'] = ($votes / $last['votes'] * 100)."%";
		}
		return $result;
	}

	public function fetch_rank_by_id( $id )
	{
		$rows	=	$this->where( "id = %d", array( $id ) )->find();
		$votes	=	$rows['votes'];
		$result = 	$this->fetch_rank_by_votes( $votes );
		$result['votes']	=	$votes;

		return	$result;
	}

	public function insert( $datas )
	{
		$result = array( "success"	=>	false,	"data"	=>	array(), "validate" => array() );

		$show = $this->create( $datas );
		$id	  = 0;

		if( !$show ){
			$result['validate'] = $this->getError();
		}else{
			$id	=	$this->add();
		}

		if( $id )
		{
			$result['success']	=	true;
			$result['data']	=	$this->where( "id=%d", array( $id ) )->select();
		}

		return $result;
	}

	public function update( $id, $datas )
	{
		$result = array();
		//$row = $this->where( "id=%d", array( $id ) )->data( $datas );
		$row = $this->create( $datas );//编辑数据
		if( !$row ){
			$result['validate'] = $this->getError();
		}else{
			$result = $row->save();
		}

		return $result;
	}

	public function update_by_uid( $uid, $datas )//TODO: confusion
	{
		$result = $this->where( "uid=%d", array( $uid ) )
			->data( $datas )
			->save();

		return $result;
	}

	public function update_click()
	{
	}

	public function update_share()
	{
	}

	public function update_votes( $id )
	{
		$this->startTrans();
		//TODO:事务锁
		$row = $this->field( "uid" )->where( "id = %d", array( $id ) )->find();
		$updated_a = $this->where( "id = %d", array( $id ) )->setInc( "votes" );//本作品

		$user = D( "teacher" );//TODO:teacher
		$updated_b  = $user->where( "id = %d", array( $row['uid'] ) )->setInc( "votes" );//本作品指导老师票数增加

		if( !$updated_a || !$updated_b ){
			$this->rollback();
			return false;
		}else{
			$this->commit();
			return true;
		}

	}

	public function update_vote( $id )
	{
		$updated_a = false;
		$updated_b = false;
		$updated_c = false;
		$updated_d = false;
		$updated_e = false;
		$updated_f = false;

		$this->startTrans();
		//TODO:事务锁
		$rowp = $this->field( "votes", "rank", 'uid' )->where( "id = %d", array( $id ) )->find();//本作品
		$rowq = $this->field( "votes", "rank" )->where( "rank = %d", array( $rowp['rank'] - 1 ? $rowp['rank'] - 1 : 1 ) )->find();//排名+1作品

		$updated_a = $this->where( "id = %d", array( $id ) )->save( array( "votes" => $rowp['votes'] + 1 ) );//更新本作品的票数;
		if( $rowp['votes'] > $rowq['votes'] ){//比较票数更新排名、票数差
			$updated_b = $this->where( "id = %d", array( $id ) )->save( array( "rank" => $rowq['rank'], "votes_gap" => $rowq['votes'] ) );
			$updated_c = $this->where( "rank = %d", array( $rowq['rank'] ) )->save( array( "rank" => $rowp['rank'], "votes_gap" => 1 ) );
		}else if( $rowp['votes'] == $rowq['votes'] ){
			$updated_b = $this->where( "id = %d", array( $id ) )->save( array( "rank" => $rowq['rank'], "votes_gap" => $rowq['votes'] ) );
			$updated_c = $updated_b;
		}else{
			$updated_b = $this->where( "id = %d", array( $id ) )->save( array( "votes_gap" => $rowq['votes'] - $rowp['votes'] ) );
			$updated_c = $updated_b;
		}

		$user = D( "teacher" );//TODO:teacher
		$row_userp  = $user->field( "votes", "rank" )->where( "id = %d", array( $rowp['uid'] ) )->find();//本作品指导老师
		$row_userq  = $user->field( "votes", "rank" )->where( "rank = %d", array( $row_userp['rank'] - 1 ) )->find();//排名+1指导老师
		$updated_d  = $this->where( "id = %d", array( $rowp['uid'] ) )->save( array( "votes" => $row_userp['votes'] + 1 ) );//更新相关指导老师的票数
		if( $row_userp['votes'] > $row_userq['votes'] ){//比较相关教排名
			$updated_e = $user->where( "id = %d", array( $rowp['uid'] ) )->save( array( "rank" => $row_userq['rank'] ) );
			$updated_f = $user->where( "rank = %d", array( $row_userp['rank'] - 1 ) )->save( array( "rank" => $row_userp['rank'] ) );
		}else if( $row_userp['votes'] == $row_userq['votes'] ){
			$updated_e = $user->where( "id = %d", array( $rowp['uid'] ) )->save( array( "rank" => $row_userq['rank'] ) );
			$updated_f = $updated_e;
		}else{
			$updated_e = true;
			$updated_f = true;
		}

		if( !$updated_a || !$updated_b || !$updated_c || !$updated_d || !$updated_e || !$updated_f ){
			$this->rollback();
			return false;
		}else{
			$this->commit();
			return true;
		}
	}
}
?>
